package com.keyou.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.keyou.reggie.common.BaseContext;
import com.keyou.reggie.common.CustomException;
import com.keyou.reggie.common.ResultInfo;
import com.keyou.reggie.entity.AddressBook;
import com.keyou.reggie.service.AddressBookService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author: klq
 * @Date: 2023/1/12 17:04
 * @Description:
 */
@Slf4j
@RestController
@RequestMapping("/addressBook")
public class AddressBookController {

    @Autowired
    private AddressBookService addressBookService;

    /**
     * 获取地址列表
     *
     * @param addressBook 前端无需参数
     * @return
     */
    @GetMapping("/list")
    public ResultInfo<List<AddressBook>> list(AddressBook addressBook) {
        //后端获取登陆的用户ID，并根据userId获取对应地址数据集
        addressBook.setUserId(BaseContext.getCurrentId());
        log.info("addressBook={}", addressBook);

        //条件构造器
        LambdaQueryWrapper<AddressBook> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(addressBook.getUserId() != null, AddressBook::getUserId, addressBook.getUserId());
        queryWrapper.orderByDesc(AddressBook::getUpdateTime);

        List<AddressBook> addressBooks = addressBookService.list(queryWrapper);
        return ResultInfo.success(addressBooks);
    }

    /**
     * 添加收货地址
     *
     * @param addressBook
     * @return
     */
    @PostMapping
    public ResultInfo<AddressBook> addAddress(@RequestBody AddressBook addressBook) {
        addressBook.setUserId(BaseContext.getCurrentId());
        log.info("添加收货地址，addressBook:{}", addressBook);
        addressBookService.save(addressBook);
        return ResultInfo.success(addressBook);
    }

    /**
     * 选择默认收货地址
     *
     * @param addressBook
     * @return
     */
    @PutMapping("/default")
    public ResultInfo<AddressBook> setDefaultAddress(@RequestBody AddressBook addressBook) {
        //获取当前用户id
        addressBook.setUserId(BaseContext.getCurrentId());
        //注意这里的条件构造器是LambdaUpdateWrapper，而不是我们前面经常用的LambdaQueryWrapper
        LambdaUpdateWrapper<AddressBook> queryWrapper = new LambdaUpdateWrapper<>();
        //条件：当前用户的地址
        queryWrapper.eq(addressBook.getUserId() != null, AddressBook::getUserId, addressBook.getUserId());
        //将当前用户地址的is_default字段全部设为0
        queryWrapper.set(AddressBook::getIsDefault, 0);
        //执行更新操作
        addressBookService.update(queryWrapper);

        //随后再将当前地址的is_default字段设为1
        addressBook.setIsDefault(1);
        //再次执行更新操作
        addressBookService.updateById(addressBook);
        return ResultInfo.success(addressBook);
    }

    /**
     * 获取当前用户的收货地址
     *
     * @return
     */
    @GetMapping("/default")
    public ResultInfo<AddressBook> defaultAddress() {
        //获取当前用户id
        Long userId = BaseContext.getCurrentId();
        //条件构造器
        LambdaQueryWrapper<AddressBook> queryWrapper = new LambdaQueryWrapper<>();
        //当前用户
        queryWrapper.eq(userId != null, AddressBook::getUserId, userId);
        //默认地址
        queryWrapper.eq(AddressBook::getIsDefault, 1);
        AddressBook addressBook = addressBookService.getOne(queryWrapper);
        return ResultInfo.success(addressBook);
    }

    /**
     * 根据id获取地址信息
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public ResultInfo<AddressBook> getById(@PathVariable Long id) {
        AddressBook addressBook = addressBookService.getById(id);
        if (addressBook == null) {
            throw new CustomException("地址信息不存在");
        }
        return ResultInfo.success(addressBook);
    }

    /**
     * 修改地址信息
     *
     * @param addressBook
     * @return
     */
    @PutMapping
    public ResultInfo<String> updateAdd(@RequestBody AddressBook addressBook) {
        if (addressBook == null) {
            throw new CustomException("地址信息不存在，请刷新重试");
        }
        addressBookService.updateById(addressBook);
        return ResultInfo.success(null, "地址修改成功");
    }

    /**
     * 删除地址信息
     *
     * @param id
     * @return
     */
    @DeleteMapping()
    public ResultInfo<String> deleteAdd(@RequestParam("ids") Long id) {
        if (id == null) {
            throw new CustomException("地址信息不存在，请刷新重试");
        }
        AddressBook addressBook = addressBookService.getById(id);
        if (addressBook == null) {
            throw new CustomException("地址信息不存在，请刷新重试");
        }
        addressBookService.removeById(id);
        return ResultInfo.success("地址删除成功");
    }

}